//
//  HospitalListVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/14.
//  Copyright © 2016年 阳光. All rights reserved.
//  医院列表界面

#import "HospitalListVC.h"
#import "HospitalListModel.h"

#import "CountryListModel.h"
#import "AreaListModel.h"
#import "ConfirmVC.h"

static NSString *tableViewCellIdentifier=@"UITableViewCellIdentifier";

@interface HospitalListVC ()<UITableViewDataSource,UITableViewDelegate,CustomNavViewDelegate>

@property(nonatomic,strong)UITableView *hospitalListTableView;

@property(nonatomic,strong)NSMutableArray *hospitalListArr;

@end



@implementation HospitalListVC

-(void)leftBtnEvent
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)rightBtnEvent
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _hospitalListArr=[NSMutableArray array];
    
    CustomNavView *hospitalListNavView=[[CustomNavView alloc]initWithTitle:NSLocalizedString(@"Select for hospital", nil) leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];//@"选择所在医院"
    hospitalListNavView.delegate=self;
    hospitalListNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:hospitalListNavView];
    
    [self initTableView];
    
    // 获取医院列表数据
    [self getHospitalListData];
    
}

// 获取医院列表数据
-(void)getHospitalListData
{
    [self.hospitalListArr removeAllObjects];
    
    __weak typeof(self) weakSelf=self;
    
    NSDictionary *dic=@{
                        @"sid":_areaID
                        };
    
    [QBRequest GetHospitalInfoWithDic:dic Success:^(id res)
     {
      weakSelf.hospitalListArr=[AreaListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];

      [weakSelf.hospitalListTableView reloadData];

      [weakSelf.hospitalListTableView.mj_header endRefreshing];
         
    } failure:^(NSString *error)
    {
        
    }];
}

-(void)initTableView
{
    // _hospitalListTableView
    _hospitalListTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,64,APPW,APPH-64) style:UITableViewStylePlain];
    _hospitalListTableView.delegate=self;
    _hospitalListTableView.dataSource=self;
    _hospitalListTableView.hidden=NO;
    _hospitalListTableView.backgroundColor=WhiteColor;
    _hospitalListTableView.showsVerticalScrollIndicator=NO;
    _hospitalListTableView.tableFooterView=[UIView new];
    _hospitalListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_hospitalListTableView];
    [_hospitalListTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.hospitalListArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    AreaListModel *countryListModel=self.hospitalListArr[indexPath.row];
    
    BOOL isCN = [[LanguageConvert alloc] hasShownIsCnOrNot];
    if (isCN == YES) {//..
        cell.textLabel.text=countryListModel.cn_name;
    } else{
        cell.textLabel.text=countryListModel.en_name;
    }
//    cell.textLabel.text=countryListModel.cn_name;
    
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100/2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    AreaListModel *areaListModel=self.hospitalListArr[indexPath.row];
    
    
    NSDictionary *dic = [NSDictionary dictionary];
    BOOL isCN = [[LanguageConvert alloc] hasShownIsCnOrNot];
    if (isCN == YES) {//..
        dic=[[NSDictionary alloc]initWithObjectsAndKeys:areaListModel.cn_name,@"hospitalName",areaListModel.areaID,@"hospitalID",_areaID,@"areaID",nil];
    } else {
        dic=[[NSDictionary alloc]initWithObjectsAndKeys:areaListModel.en_name,@"hospitalName",areaListModel.areaID,@"hospitalID",_areaID,@"areaID",nil];
    }
    //NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:areaListModel.cn_name,@"hospitalName",areaListModel.areaID,@"hospitalID",_areaID,@"areaID",nil];
    
    // 创建通知
    NSNotification *notification=[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dic];
    // 通过通知中心发送通知
    [[NSNotificationCenter defaultCenter]postNotification:notification];

    
    [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:nil];
  
}

@end
